/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simulateurs.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import simulateurs.model.Model;
import simulateurs.view.controllersView.ButtonsPanelView;
import simulateurs.view.controllersView.ControllersPanelView;
import simulateurs.view.modelView.ModelPanelView;

public class View extends JFrame implements I_View, KeyListener {
	private String TITLE="Naze Evolution Soccer";
	private ButtonsPanelView buttonsPanel=null;
	private ModelPanelView modelPanel=null;
	private ControllersPanelView controllersPanel=null;
	private Model model=null;
	
	public View(Model model){
		this.model=model;
		initWindow();
	}

	private void initWindow(){
		this.setTitle(TITLE);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});

		this.modelPanel=new ModelPanelView(this); // il faut un nouveau parametre pour fixer la taille !
		this.controllersPanel=new ControllersPanelView(this);
		this.buttonsPanel=new ButtonsPanelView(this);
		
		this.setLayout(new BorderLayout());
		this.add(this.modelPanel,BorderLayout.CENTER);
		this.add(this.controllersPanel, BorderLayout.EAST);
		this.add(this.buttonsPanel,BorderLayout.SOUTH);
		
		this.addKeyListener(this);
		this.getRootPane().setDefaultButton(this.buttonsPanel.getDemarrer());
		if(System.getProperty("os.name").toLowerCase().indexOf("win")>=0) this.setSize(this.modelPanel.getWidth()+110,this.modelPanel.getHeight()+55);
		else this.setSize(this.modelPanel.getWidth()+130,this.modelPanel.getHeight()+27);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize(); 
	    Dimension frameSize=this.getSize();
		if(frameSize.height>screenSize.height) frameSize.height=screenSize.height; 
        if(frameSize.width>screenSize.width) frameSize.width=screenSize.width;
		this.setLocation((screenSize.width-frameSize.width)/2, (screenSize.height-frameSize.height)/2); 
		this.setVisible(true);
	}

	@Override
	public void repeindre() {
		modelPanel.repeindre();
		buttonsPanel.repeindre();
		controllersPanel.repeindre();
	}
	
    public ButtonsPanelView getButtonsPanel() {
        return buttonsPanel;
    }

    public ControllersPanelView getControllersPanel() {
        return controllersPanel;
    }

    public ModelPanelView getModelPanel() {
        return modelPanel;
    }

	public Model getModel() {
		return model;
	}
	
	private void performEnter(){
		if(this.buttonsPanel.getDemarrer().isEnabled()) this.buttonsPanel.performDemarrer();
		else this.buttonsPanel.performPauser();
	}
	
//	private void performS(){
//		this.buttonsPanel.performStopper();
//	}
//	
//	private void performR(){
//		this.buttonsPanel.performReset();
//	}

	@Override
	public void keyTyped(KeyEvent e) {
		if(e.getKeyCode()==e.VK_ENTER) performEnter();
//		if(e.getKeyCode()==e.VK_S) performS();
//		if(e.getKeyCode()==e.VK_R) performR();
	}

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}
}
